using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;

namespace PointBridge.SPDX
{
    public class OptionsSetCtrl : System.Web.UI.UserControl
    {
        protected Table wOptionsSetTable;
        
        protected void Page_Load(object sender, EventArgs e){}
        public void InitOptions(OptionsSet optionsSet, int columns)
        {
            int xPos = 1;
            TableRow row = null;
            for (int i = 0; i < optionsSet.count; i++)
            {
                if (xPos == 1) row = new TableRow();
                TableCell cell = new TableCell();
                CheckBox checkBox = new CheckBox();
                checkBox.ID = "OptionCheckbox" + i;
                checkBox.Text = optionsSet[i].optionName.Replace(" ", "&nbsp;");
                checkBox.Checked = optionsSet[i].optionValue;
                checkBox.CssClass = "ms-formbody";
                checkBox.ForeColor = optionsSet[i].color;
                checkBox.Font.Size = FontUnit.Point(8);
                cell.Controls.Add(checkBox);
                cell.Attributes.Add("width", (100 / columns).ToString() + "%");
                if (row == null) throw new PointBridgeEx("Unexpected currentRow = null: X1");
                row.Cells.Add(cell);
                if ((xPos >= columns) || (i == optionsSet.count - 1) /*|| (optionsSet[i].color != optionsSet[i + 1].color)*/) //if we are at the end of a normal row or this is the end of the last row
                {
                    if (row == null) throw new PointBridgeEx("Unexpected currentRow = null: X2");
                    wOptionsSetTable.Rows.Add(row);
                    xPos = 1;
                }
                else
                {
                    xPos++;
                }
            }
        }
        public void Reset() { }//do nothing b/c these are all checkboxes
        public OptionsSet Parse(bool strict, OptionsSet optionsSet)
        {
            for (int i = 0; i < optionsSet.count; i++)
            {
                CheckBox checkBox = (CheckBox)FindControl("OptionCheckbox" + i);
                optionsSet[i].optionValue = checkBox.Checked;
            }
            //optionsSet.ValidateOneOptionSetEx();
            return optionsSet;
        }
    }
}
